package utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.apache.http.HttpResponse;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.impl.client.DefaultHttpClient;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;

import android.os.AsyncTask;
import android.util.Log;

public class ServerRequest extends AsyncTask<Object, Object, Document>{

	
//	public ServerRequest(HttpUriRequest method) {
//		this.method = method;
////		if(type.equals(GET_TYPE_CONSTATNT)) {
////			getMethod = new HttpGet("http://api-fotki.yandex.ru/api/me/");
////			getMethod.addHeader("Authorization","OAuth "+token);
////		
////			
////		} else if(type.equals(POST_TYPE_CONSTANT)) {
////			postMethod = new HttpPost(serverUri);
////			try {
////				postMethod.setEntity(new UrlEncodedFormEntity(params));
////				Mul
////				
////			} catch (UnsupportedEncodingException e) {
////				e.printStackTrace();
////			}
////		}
//	}

	@Override
	protected void onPreExecute() {
		// TODO Auto-generated method stub
		super.onPreExecute();
	}

	@Override
	protected Document doInBackground(Object... params) {
		HttpClient client = new DefaultHttpClient();
		try {
			HttpResponse response = client.execute(method);
			
			int statusCode = response.getStatusLine().getStatusCode();
			Log.d("yandex",Integer.toString(statusCode));
			if(statusCode == 200 ||statusCode == 201){ 
				 try {
					//JSONObject jsonResponse =(JSONObject)(new JSONTokener(EntityUtils.toString(response.getEntity())).nextValue());
					InputStream ins = response.getEntity().getContent();
//					BufferedReader in = new BufferedReader(new InputStreamReader(ins));
					
		            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
					try {
						DocumentBuilder builder = factory.newDocumentBuilder();
						Document doc = builder.parse(ins);
//						StringBuffer sb = new StringBuffer("");
//			            String line = "";
//			            String NL = System.getProperty("line.separator");
//			            while ((line = in.readLine()) != null) {
//			                sb.append(line + NL);
//			            }
//			            Log.d("yandex","asd"+sb.toString());
						return doc;
					} catch (ParserConfigurationException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (IllegalStateException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (SAXException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					
					
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} 
				
			}
			
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	@Override
	protected void onPostExecute(Document result) {
		
	}

	
	protected HttpUriRequest method;
	
	
//	private final String GET_TYPE_CONSTATNT = "GET";
//	private final String POST_TYPE_CONSTANT = "POST";
	
}
